package com.sgb.goods.viewmodel;

import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ClipboardUtils;
import com.sgb.goods.R;
import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.PayDetailEntity;
import com.sgb.goods.entity.PersonalPayDetailEntity;
import com.sgb.goods.entity.order.OrderPayDetailEntity;
import com.sgb.goods.entity.order.OrderPayInfoEntity;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.adapter.order.OrderPayDetailAdapter;
import com.sgb.goods.view.adapter.order.PayInformationAdapter;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.MToast;

import java.util.List;

/**
 * 订单详情-付款信息
 *
 * @author cyj
 * <p>
 * 时间2021.12.20
 */
public class PayInformationViewModel extends BaseViewModel {

    public OrderPayDetailAdapter adapter;
    public PayInformationAdapter payInformationAdapter;
    /**
     * 1：采购订单；2：销售订单。
     */
    public ObservableField<String> orderType = new ObservableField<>();

    /**
     * 收款账户
     */
    public ObservableField<String> payeeAccountName = new ObservableField<>();
    /**
     * 收款账号
     */
    public ObservableField<String> payeeAccountNumber = new ObservableField<>();
    /**
     * 开户银行
     */
    public ObservableField<String> payeeAccountBankName = new ObservableField<>();

    /**
     * 支付详情弹窗
     */
    public MutableLiveData<PayDetailEntity> paymentDetailsList = new MutableLiveData<>();
    /**
     * 付款约定弹窗
     */
    public MutableLiveData<List<OrderPayDetailEntity>> orderPayDetailEntity = new MutableLiveData<>();

    /**
     * 更新付款信息页面
     */
    public MutableLiveData<List<OrderPayDetailEntity>> cOrderPayDetailEntity = new MutableLiveData<>();
    /**
     * 更新付款约定页面
     */
    public MutableLiveData<List<OrderPayDetailEntity>> orderPayAgreementEntity = new MutableLiveData<>();

    public PayInformationViewModel() {
        payInformationAdapter = new PayInformationAdapter(R.layout.goods_item_order_del_payment_info);
        adapter = new OrderPayDetailAdapter(R.layout.goods_item_order_payment_info);
    }

    /**
     * 复制订单号到粘贴板 点击事件
     */
    public void copyToClipboard() {
        ClipboardUtils.copyText(payeeAccountNumber.get());
        MToast.showToast(application.getApplicationContext(), "复制成功");
    }

    /**
     * 付款详情请求
     *
     * @param orderType         1：采购订单；2：销售订单。
     * @param paymentMethod     支付方式 0：网银支付 1：银行转账
     * @param orderProcessState 订单流程状态 1：待接单 2：待确认 3：履约中 4：已完成 5：已取消
     */
    public void getListPayDetails(String orderId, String orderType, int paymentMethod, int orderProcessState) {
        GoodsNet.getInstance().getGoodsApi().getListPayDetails(orderId, orderType, "2")
                .enqueue(new GoodsCallBack<BaseEntity<OrderPayInfoEntity>>() {
                    @Override
                    public void onSuccess(BaseEntity<OrderPayInfoEntity> data) {
                        adapter.setIsDaiQueRen(orderProcessState);
                        adapter.setIsOnlinePayment(paymentMethod);
                        adapter.setIsPurchaseOrder(orderType);
                        payeeAccountName.set(data.getData().payeeAccountName);
                        payeeAccountNumber.set(data.getData().payeeAccountNumber);
                        payeeAccountBankName.set(data.getData().payeeAccountBankName);
                        adapter.setNewData(data.getData().pays);
                        cOrderPayDetailEntity.postValue(data.getData().pays);
                    }
                });
    }

    /**
     * 获取付款详情 （资金中心接口）
     *
     * @param payId         支付ID
     * @param payStateDesc  支付状态
     * @param orderIdentity
     */
    public void getPaymentDetails(String payId, String payStateDesc, int orderIdentity) {
        showLoading();
        if (orderIdentity == 1) {
            GoodsNet.getInstance().getCapitalApi().getPayOrderDetails(payId).enqueue(new GoodsCallBack<BaseEntity<PayDetailEntity>>() {
                @Override
                public void onSuccess(BaseEntity<PayDetailEntity> data) {
                    data.getData().payStateDesc = payStateDesc;
                    paymentDetailsList.setValue(data.getData());
                }
            });
        } else if (orderIdentity == 0) {
            GoodsNet.getInstance().getCapitalApi().getPersonalPayOrderDetails(payId).enqueue(new GoodsCallBack<BaseEntity<PersonalPayDetailEntity>>() {
                @Override
                public void onSuccess(BaseEntity<PersonalPayDetailEntity> data) {
                    if (null == data || null == data.getData()) {
                        return;
                    }
                    PersonalPayDetailEntity detailEntity = data.getData();
                    PayDetailEntity payDetailEntity = new PayDetailEntity();
                    payDetailEntity.method = "在线付款";
                    payDetailEntity.number = detailEntity.flowNo;
                    payDetailEntity.payType = detailEntity.payType;
                    payDetailEntity.channel = detailEntity.payChannels;
                    payDetailEntity.money = detailEntity.tradeAmount;
                    payDetailEntity.fees = detailEntity.tradeFees;
                    payDetailEntity.payStateDesc = getPayStateDescribe(detailEntity.tradeStatus);
                    payDetailEntity.payerCompanyName = detailEntity.payName;
                    payDetailEntity.orderId = detailEntity.orderId;
                    payDetailEntity.created = detailEntity.created;
                    payDetailEntity.completionTime = detailEntity.payTime;
                    paymentDetailsList.setValue(payDetailEntity);
                }
            });
        }
    }

    /**
     * @param payCode （1：.已成功，2.已失败，3.冻结中,4.确认中）
     * @return 对应的状态描述
     */
    private String getPayStateDescribe(int payCode) {
        if (payCode == 1) {
            return "已成功";
        } else if (payCode == 2) {
            return "已失败";
        } else if (payCode == 3) {
            return "冻结中";
        } else if (payCode == 4) {
            return "确认中";
        } else {
            return "待付款";
        }
    }

    /**
     * 获取付款约定
     */
    public void getPaymentAgreementList(String orderId, boolean isDisplayWindow) {
        showLoading();
        GoodsNet.getInstance().getGoodsApi().getPayAppointList(orderId).enqueue(new GoodsCallBack<BaseEntity<List<OrderPayDetailEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<OrderPayDetailEntity>> data) {
                if (isDisplayWindow) {
                    orderPayDetailEntity.postValue(data.getData());
                } else {
                    payInformationAdapter.setNewData(data.getData());
                    orderPayAgreementEntity.postValue(data.getData());
                }
            }

            @Override
            public void onFail() {
                super.onFail();
            }
        });
    }
}
